package com.codedancer.campus.manager.api.menu;

import cn.hutool.core.bean.BeanUtil;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.menu.ro.MenuAddOrUpdateRo;
import com.codedancer.campus.manager.service.IMenuService;
import com.codedancer.campus.manager.service.bo.menu.MenuAddOrUpdateBo;
import com.codedancer.campus.manager.service.vo.menu.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/menu")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "菜单Api", description = "菜单Api", tags = "菜单Api")
public class MenuApi extends BaseApi {

    @Autowired
    private IMenuService menuService;

    @PostMapping("/add")
    @ApiOperation("添加菜单")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> add(
        @RequestBody @Valid @ApiParam(value = "添加菜单Ro", required = true)
        MenuAddOrUpdateRo menuAddOrUpdateRo) {
        MenuAddOrUpdateBo menuAddOrUpdateBo = BeanUtil.copyProperties(menuAddOrUpdateRo,
            MenuAddOrUpdateBo.class);
        menuService.add(menuAddOrUpdateBo);
        return success();
    }

    @GetMapping("/list-by-patent-uuid/{parentUuid}")
    @ApiOperation("根据父级uuid获取下级菜单")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<List<MenuVO>> listByParentUuid(
        @PathVariable(value = "parentUuid", required = true) @ApiParam(value = "父级菜单uuid", required = true) String parentUuid) {
        return success(menuService.listByParentUuid(parentUuid));
    }

    @PostMapping("/add-children/{parentUuid}")
    @ApiOperation("添加子集菜单")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<MenuVO> addChildren(
        @RequestBody @Valid @ApiParam(value = "添加菜单Ro", required = true)
        MenuAddOrUpdateRo menuAddOrUpdateRo,
        @PathVariable(value = "parentUuid", required = true) @ApiParam(value = "父级菜单uuid", required = true) String parentUuid) {
        MenuAddOrUpdateBo menuAddOrUpdateBo = BeanUtil.copyProperties(menuAddOrUpdateRo,
            MenuAddOrUpdateBo.class);
        menuAddOrUpdateBo.setParentUuid(parentUuid);
        return success(menuService.addChildren(menuAddOrUpdateBo));
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除菜单")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "菜单uuid", required = true) String uuid) {
        menuService.delete(uuid);
        return success();
    }

    @PutMapping("/update/{uuid}")
    @ApiOperation("修改菜单")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> update(
        @RequestBody @Valid @ApiParam(value = "修改菜单Ro", required = true)
        MenuAddOrUpdateRo menuAddOrUpdateRo,
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "菜单uuid", required = true) String uuid) {
        MenuAddOrUpdateBo menuAddOrUpdateBo = BeanUtil.copyProperties(menuAddOrUpdateRo,
            MenuAddOrUpdateBo.class);
        menuAddOrUpdateBo.setUuid(uuid);
        menuService.update(menuAddOrUpdateBo);
        return success();
    }

    @PutMapping("/move-up/{uuid}")
    @ApiOperation("上移")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> moveUp(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "菜单uuid", required = true) String uuid) {
        menuService.moveUp(uuid);
        return success();
    }

    @PutMapping("/move-down/{uuid}")
    @ApiOperation("下移")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> moveDown(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "菜单uuid", required = true) String uuid) {
        menuService.moveDown(uuid);
        return success();
    }
}
